﻿Imports FinanceiroBLL
Imports FinanceiroDAL.ModuloConexao
Public Class PrestadoresUC
    Dim objBLL As New PrestadoresBLL
#Region "Voltar"
    Private Sub TSBVoltar_Click(sender As System.Object, e As System.EventArgs) Handles TSBVoltar.Click
        statusfinanceiro = "N"
        Dispose()
    End Sub
#End Region
#Region "Load"
    Private Sub PrestadoresUC_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        PLocalizar.Visible = False
        CBFiltro.SelectedIndex = 1
        CBFiltro.Focus()
        carregagrid()
    End Sub
#End Region
#Region "Carrega Grid"
    Sub carregagrid()
        DGVDados.DataSource = objBLL.buscar("", 7)
        DGVDados.Refresh()
    End Sub
#End Region
#Region "Novo"
    Private Sub TSBNovo_Click(sender As System.Object, e As System.EventArgs) Handles TSBNovo.Click
        Try
            Dim frm As New PrestadoresFRM
            frm.Op = "I"
            TSBNovo.BackColor = Color.Gainsboro
            frm.ShowDialog()
            frm.Dispose()
            frm = Nothing
            carregagrid()
        Catch ex As Exception
        Finally
            TSBNovo.BackColor = Color.Transparent
        End Try

    End Sub
#End Region
#Region "Alterar"
    Private Sub TSBAlterar_Click(sender As System.Object, e As System.EventArgs) Handles TSBAlterar.Click

        Dim indice As Integer = -1
        Try
            For Each linha As DataGridViewRow In DGVDados.SelectedRows
                If (Not linha.Cells("pre_codigo") Is Nothing) Then
                    indice = linha.Index
                End If
            Next
            If (indice <> -1) Then
                Dim frma As New PrestadoresFRM
                frma.codigo = Val(DGVDados.Rows(indice).Cells("pre_codigo").Value)
                frma.Op = "A"
                TSBAlterar.BackColor = Color.Gainsboro
                frma.ShowDialog()
                frma.Dispose()
                frma = Nothing
                carregagrid()
            Else
                MessageBox.Show("Selecione o Prestador a ser realizado á alteração", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            TSBAlterar.BackColor = Color.Transparent
        End Try
    End Sub
#End Region
#Region "Localizar"
    Private Sub TXTLocalizar_TextChanged(sender As System.Object, e As System.EventArgs)
        Select Case CBFiltro.Text
            Case "Código"
                DGVDados.DataSource = objBLL.buscar(TXTLocalizar.Text, CBFiltro.SelectedIndex)
            Case "Nome"
                DGVDados.DataSource = objBLL.buscar(TXTLocalizar.Text, CBFiltro.SelectedIndex)
            Case "CPF"
                DGVDados.DataSource = objBLL.buscar(TXTLocalizar.Text, CBFiltro.SelectedIndex)
            Case "CNPJ"
                DGVDados.DataSource = objBLL.buscar(TXTLocalizar.Text, CBFiltro.SelectedIndex)
                'Case "Status"
                '    DGVDados.DataSource = objBLL.buscar(TXTLocalizar.Text, CBFiltro.SelectedIndex)
        End Select
        If TXTLocalizar.Text = "" Then
            DGVDados.DataSource = objBLL.buscar("", 7)
        End If
        DGVDados.Refresh()
    End Sub
#End Region
#Region "Excluir"
    Private Sub TSBExcluir_Click(sender As System.Object, e As System.EventArgs) Handles TSBExcluir.Click
        Try
            Dim cod As Integer = 0
            Dim indice As Integer = -1
            For Each linha As DataGridViewRow In DGVDados.SelectedRows
                If (Not linha.Cells("pre_codigo") Is Nothing) Then
                    objBLL = New PrestadoresBLL(Val(DGVDados.Rows(linha.Index).Cells("pre_codigo").Value.ToString()))
                End If
            Next
            If (objBLL.pre_codigo > 0) Then
                TSBExcluir.BackColor = Color.Gainsboro
                If MessageBox.Show("Confirma exclusão do Prestador " & objBLL.pre_codigo & " ?", "", MessageBoxButtons.YesNo) = vbYes Then
                    objBLL.sta_codigo = 3
                    objBLL.InserirAlterar()
                    'validação de nao exclusao
                    Dim ret As Boolean
                    If ret = True Then
                        MsgBox("Exclusão efetuada com sucesso!", MsgBoxStyle.Information)
                        carregagrid()
                    End If

                End If
            Else
                MessageBox.Show("Selecione o Prestador a ser excluido", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            TSBExcluir.BackColor = Color.Transparent
        End Try
    End Sub
#End Region
#Region "Localizar"
    Private Sub TSBLocalizar_Click(sender As System.Object, e As System.EventArgs) Handles TSBLocalizar.Click
        If PLocalizar.Visible = True Then
            PLocalizar.Visible = False
            TXTLocalizar.Text = ""
            TSBLocalizar.BackColor = Color.Transparent
        Else
            PLocalizar.Visible = True
            TXTLocalizar.Text = ""
            TSBLocalizar.BackColor = Color.Gainsboro
        End If
    End Sub
#End Region
    Private Sub CBFiltro_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles CBFiltro.SelectedIndexChanged
        If CBFiltro.SelectedIndex = 4 Then
            CBStatus.Visible = True
            TXTLocalizar.Visible = False
            CBStatus.SelectedIndex = 0
        Else
            CBStatus.Visible = False
            TXTLocalizar.Visible = True
        End If
        TXTLocalizar.Text = ""
        carregagrid()
    End Sub

#Region "Cor de fundo do datagridview"
    Private Sub DGVDados_RowPrePaint(sender As System.Object, e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles DGVDados.RowPrePaint
        If DGVDados.Rows(e.RowIndex).Index Mod (2) = 1 Then
            DGVDados.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.Gainsboro
        End If
    End Sub
#End Region
#Region "Detalhes"
    Private Sub TSBDetalhes_Click(sender As System.Object, e As System.EventArgs) Handles TSBDetalhes.Click
        For Each linha As DataGridViewRow In DGVDados.SelectedRows
            If (Not linha.Cells("pre_codigo") Is Nothing) Then
                objBLL = New PrestadoresBLL(Val(DGVDados.Rows(linha.Index).Cells("pre_codigo").Value.ToString()))
            End If
        Next
        If (Not IsNothing(objBLL)) Then
            If (objBLL.pre_codigo > 0) Then gerarelatrio(objBLL.pre_descricao, "{vw_FinPrestadores.pre_codigo}=" & objBLL.pre_codigo)
        End If
    End Sub
    Public Sub gerarelatrio(titulo As String, formula As String)
        Try
            Dim frmrecursos As New RelatoriosfinFRM
            Dim rpt As New PrestadoresDetalhado

            Try
                rpt.SetDatabaseLogon(usuario, senha)
                rpt.SetParameterValue(0, titulo)
                rpt.RecordSelectionFormula = formula
                frmrecursos.CrystalReportViewer2.ShowGroupTreeButton = False
                frmrecursos.CrystalReportViewer2.ReportSource = rpt
                frmrecursos.ShowDialog()
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
            frmrecursos.Dispose()
            rpt = Nothing
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Finally

        End Try
    End Sub
#End Region

    Private Sub CBStatus_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles CBStatus.SelectedIndexChanged
        DGVDados.DataSource = objBLL.buscar(CBStatus.Text, CBFiltro.SelectedIndex)
    End Sub
End Class
